package com.poisonh.unifiedauth.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lzy.okhttputils.model.HttpParams;
import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.adapter.RvSupportedAppAdapter;
import com.poisonh.unifiedauth.model.SupportedAppModel;
import com.poisonh.unifiedauth.ui.svprogresshud.SVProgressHUD;
import com.poisonh.unifiedauth.utils.ConstantUitls;
import com.poisonh.unifiedauth.utils.GsonUtils;
import com.poisonh.unifiedauth.utils.MyHttpsUtils;
import com.poisonh.unifiedauth.utils.ToastUtils;
import com.poisonh.unifiedauth.widget.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * 已支持应用
 * Created by PoisonH on 2016/7/29.
 */
public class SupportedAppActivity extends BaseActivity implements View.OnClickListener
{
    private XRecyclerView mRvSupportedApp;
    private RvSupportedAppAdapter mRvSupportedAppAdapter;
    private List<SupportedAppModel> mList;
    private int page = 1;
    private boolean isLoad = false;

    @Override
    protected void loadContentView(Bundle savedInstanceState)
    {
        setContentView(R.layout.activity_devicemange);
        initTitleView();
        initView();
    }

    @Override
    protected void initView()
    {
        setTitleTxt("已支持应用");
        mRightBtn.setVisibility(View.GONE);
        mLeftBtn.setOnClickListener(this);
        mList = new ArrayList<>();
        mRvSupportedApp = (XRecyclerView) findViewById(R.id.xrv_devicemange);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRvSupportedApp.setLayoutManager(layoutManager);
        mRvSupportedAppAdapter = new RvSupportedAppAdapter(this);
        mRvSupportedApp.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        mRvSupportedApp.setAdapter(mRvSupportedAppAdapter);
        mRvSupportedApp.setLoadingListener(new XRecyclerView.LoadingListener()
        {
            @Override
            public void onRefresh()
            {
                getDeviceList(1);
                isLoad = false;
            }

            @Override
            public void onLoadMore()
            {
                page += 1;
                getDeviceList(page);
                isLoad = true;
            }
        });

        getDeviceList(page);
    }

    /**
     * 获取设备列表
     *
     * @param page
     */
    private void getDeviceList(int page)
    {
        SVProgressHUD.showWithStatus(SupportedAppActivity.this, "加载中...");
        HttpParams params = new HttpParams();
        params.put("id", mStrUserId);
        params.put("token", mStrUserToken);
        params.put("page", page + "");
        params.put("size", "10");
        MyHttpsUtils.post(ConstantUitls.REQUEDT_URL + ConstantUitls.GETCMANGELIST, params, new MyHttpsUtils.ResultCallback()
        {
            @Override
            public void onSuccess(String response)
            {
                Log.d("LoginActivity", response);
                Message msg = Message.obtain();
                msg.obj = response;
                mHandler.sendMessage(msg);
                mRvSupportedApp.loadMoreComplete();
                mRvSupportedApp.refreshComplete();
                SVProgressHUD.dismiss(SupportedAppActivity.this);
            }

            @Override
            public void onFailure(String failReason)
            {
                mRvSupportedApp.loadMoreComplete();
                mRvSupportedApp.refreshComplete();
                SVProgressHUD.dismiss(SupportedAppActivity.this);
            }
        });
    }

    Handler mHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            mList = GsonUtils.parseSupportedAppData((String) msg.obj);
            if (isLoad)
            {
                if (mList.size() < 0)
                {
                    mRvSupportedApp.loadMoreComplete();
                    ToastUtils.showToast(SupportedAppActivity.this, "没有更多数据");
                    mRvSupportedApp.setLoadingMoreEnabled(false);
                } else
                {
                    mRvSupportedAppAdapter.notif(mList);
                }
            } else
            {
                mRvSupportedAppAdapter.update(mList);
            }
        }
    };

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ib_left_btn:
                finish();
                break;
        }
    }
}
